/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
* Copyright 2009, Center for Advanced Information Systems,Nanyang Technological University
*
* File name: ReadCluster.java
*
* Abstract:
*
* Current Version:      0.1
* Auther:               Jin Changjiu
* Modified Date:        Jul.25,2009
*
*/
package gblend.exactquery;

/**
 *
 * @author cjjin
 */

import gblend.adjlistgraph.ALGraph;
import gblend.adjlistgraph.FsgIdEntry;
import gblend.db.Parameters;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import gblend.frequentindex.FGraph;
import java.io.BufferedInputStream;
import java.util.HashSet;
import java.util.Vector;
public class ReadCluster {

    private static FGraph cluster = null;
    private static String filename = "SIndex.txt";

    public ReadCluster(Parameters para)
    {
        filename = "data/"+para.getName()+"/"+para.getdatasize()+"k/"
                +para.geta()+para.getName()+para.getdatasize()+"k"+para.getb()+"DFIndex";

    }

    public static FGraph Clusterreading(int pid)
    {
		
        String strLine = null;
	
        File fin=new File(filename);
	
	if(fin.exists())
	{
            try {

                  BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fin));
                  BufferedReader br = new BufferedReader(new InputStreamReader(bin));
                    //Read file line by line
    		  try {

                      int vn = 0;
                      int graphnum = -1;
 
                      while ((strLine = br.readLine()) != null)
                      {
                         // Print the content on the console
                          if(strLine.equals("Cluster "+pid))
                          {
                               graphnum++;
                               strLine = br.readLine();
                               vn = Integer.parseInt(strLine);
                               cluster = new FGraph(vn);

                               for(int i = 0;i<vn;i++)
                               {
                                  strLine = br.readLine();
                                  int index = strLine.indexOf(":");
                                  // set defaoult node number = 1
                                  ALGraph node = new ALGraph(1);
                                  node.setCam(strLine.substring(0, index));
                                  int oldid = Integer.parseInt(strLine.substring(index+1));
                                  node.setGraphid(oldid);
                                  cluster.addnode(node);
                               }

                               while(!(strLine = br.readLine()).equals("End"))
                               {
                                   String[] pair = strLine.split("\\s");
                                   int src = Integer.parseInt(pair[0]);
                                   int trg = Integer.parseInt(pair[1]);
                                   cluster.addEdge(src, trg);
                                   cluster.getNode(src).setChildren(trg);
                               }
                               
                           }
                       }
                   //Close the input stream
                     br.close();
		}
                catch (IOException e)
		{
                    // TODO Auto-generated catch block
                    e.printStackTrace();
		}
              } catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
                    e.printStackTrace();
		}
	}

        return cluster;
}


  public static void Idsreading( Vector<Integer> oldlist, HashSet<FsgIdEntry> fcandlist)
  {
        String strLine = null;
	FileInputStream inStream;
	DataInputStream in;
	BufferedReader br;
	File fin=new File(filename);

        if(fin.exists())
	{
            try {

                inStream=new FileInputStream(fin);
		in = new DataInputStream(inStream);
                br = new BufferedReader(new InputStreamReader(in));
		//Read file line by line
                try {
                      for(int i=0;i<oldlist.size();i++)
                      {
                         while ((strLine = br.readLine()) != null)
                         {
                           if(strLine.contains(" "+oldlist.elementAt(i)+": "))
                           {
                               int index = strLine.indexOf(":");  
                               String[] list = strLine.substring(index+1).trim().split(",");
                               for(int j=0;j<list.length;j++)
                               {
                                   String[] sublist = list[j].trim().split("\\s");
                                   FsgIdEntry tempe = new FsgIdEntry();
                                   int lngth = sublist.length;
                                   for(int k=0;k<lngth;k++){
                                       tempe.addGraphId(Integer.parseInt(sublist[k]));
                                   }
                                   /*if(Integer.parseInt(sublist[lngth])!=0){
                                       tempe.setAtBoundary(true);
                                   }*/
                                 fcandlist.add(tempe);
                               }
                               break;
                            }
                          }
                       }
                   	//Close the input stream
                        in.close();
		    }
		    catch (IOException e)
		    {
			// TODO Auto-generated catch block
			e.printStackTrace();
                    }
             } catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
            }
	}
       
    }

}
